package com.ys.tdm.controller.item;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.tdm.bean.doc.Doc;
import com.ys.tdm.bean.item.Item;
import com.ys.tdm.bean.user.User;
import com.ys.tdm.controller.BaseController;
import com.ys.tdm.service.doc.IDocService;
import com.ys.tdm.service.item.IItemService;
import com.ys.tdm.service.module.IModuleService;
import com.ys.tdm.util.JsonUtils;
import com.ys.tdm.util.SimpleReturnVo;

/**
 * 子菜单操作
* @Title:  
* @Description:TODO
* @author Troy  
* @date 2017年12月27日 下午4:03:17 
* @version V1.0 
* @Copyright:Copyright (c) 亚厦股份2016 ~ 2017 版权所有
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController{
	private static Logger logger = Logger.getLogger(ItemController.class);

	@Resource 
	private IModuleService moduleService;
	@Resource 
	private IItemService itemService;
	@Resource 
	private IDocService docService;
	

	
	@RequestMapping("/addUI")
	public String addUI(ModelMap model, String itemName,String moduleName) {
		if (StringUtils.isNotBlank(itemName)) {
			model.addAttribute("addTitle", itemName);
		}else{
			model.addAttribute("addTitle", moduleName);
		}
		return "item_add";
	}
	
	//模块树
	/*@ResponseBody
	@RequestMapping("/ajaxTree")
	public String getModuletree(ModelMap modelMap,String moduleId) {
		List<Item> moduleTree = new ArrayList<Item>();
		List<Item> allItem = new ArrayList<Item>();
		if (StringUtils.isNotBlank(moduleId)) {
			try {
				modelMap.addAttribute("moduleId", moduleId);
				//查询某个模块下的节点
				Item item = new Item();
				item.setModuleId(moduleId);
				allItem = itemService.getAll(item);
				for (Item itemInfor : allItem) {
					//找出该模块下的父节点
					if (StringUtils.isBlank(itemInfor.getPid())) {
						itemInfor.setNodes(getItemtree(itemInfor.getId(),allItem));
						itemInfor.setText(itemInfor.getItemName());
						moduleTree.add(itemInfor);
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return JsonUtils.toJson(moduleTree);
		
	}
	
	// 递归查询模块树
	public List<Item> getItemtree(String id, List<Item> items) {
		List<Item> children = new ArrayList<Item>();
		for (Item item : items) {
			if (StringUtils.isNotBlank(item.getPid())) {
				if (id.equals(item.getPid())) {
					item.setText(item.getItemName());
					children.add(item);
				}
			}
		}
		for (Item item : children) {
			item.setNodes(getItemtree(item.getId(),items));
			item.setText(item.getText());
		}
		if (children.size() == 0) {
	        return null;
	    }
	    return children;

	}
	
	
	*/
	
	
	@ResponseBody
	@RequestMapping("/ajaxTree")
	public String getModuletree(ModelMap modelMap,Item item,HttpServletRequest request) {
		List<Item> parentItem = new ArrayList<Item>();
		List<Item> templItems = new ArrayList<Item>();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
		+ path;
		try {
			List<Doc> allDoc = docService.getAll(new Doc());
			if (StringUtils.isNotBlank(item.getModuleId())) {
				parentItem = itemService.getAll(item);
				for (Item itemInfor : parentItem) {
					itemInfor.setpId(itemInfor.getPid());
					itemInfor.setName(itemInfor.getItemName());
					itemInfor.setType("1");
					itemInfor.setIcon(basePath+"/images/folder.png");
					for (Doc doc : allDoc) {
						if (doc.getItemId().equals(itemInfor.getId())) {
							Item templItem = new Item();
							templItem.setId(doc.getId());
							templItem.setName(doc.getTitle());
							templItem.setpId(itemInfor.getId());
							templItem.setType("0");
							templItem.setIcon(basePath+"/images/docu.png");
							templItems.add(templItem);
						}
					}

				}
			}
			for (Item item2 : templItems) {
				parentItem.add(item2);
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return JsonUtils.toJson(parentItem);
		
	}
	
	// 添加模块
	@ResponseBody
	@RequestMapping("/add")
	public String add(Item item,HttpServletRequest request) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "添加失败");
		try {
			User loginUser = this.getLoginUser(request);
			if (null != loginUser && StringUtils.isNotBlank(loginUser.getName())) {
				String userName = loginUser.getName();
				item.setCreator(userName);
				item.setUpdator(userName);
				this.itemService.insertItem(item);
				returnVo = new SimpleReturnVo(SUCCESS, "添加成功");
			} else {
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("ItemController-add:", e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}
	
	// 修改标题
	@ResponseBody
	@RequestMapping("/update")
	public String update(String type, String id, String name, HttpServletRequest request) {
		SimpleReturnVo vo = new SimpleReturnVo(ERROR,"修改失败");
		if (StringUtils.isNotBlank(type)) {
			try {
				if ("1".equals(type)) {
					// 修改文件夹标题
					Item item = new Item();
					item.setItemName(name);
					item.setId(id);
					itemService.updateItem(item);
					vo.setResponseCode(SUCCESS);
					vo.setResponseMsg("修改成功");
				}else{
					//修改文档标题
					Doc doc = new Doc();
					doc.setId(id);
					doc.setTitle(name);
					docService.updateDoc(doc);
					vo.setResponseCode(SUCCESS);
					vo.setResponseMsg("修改成功");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return JsonUtils.toJson(vo);
	}

}
